package edu.eas.admin.controller;


import edu.eas.admin.service.OauthClientDetailsService;
import edu.eas.common.api.CommonPage;
import edu.eas.common.api.CommonResult;
import edu.eas.common.dto.ClientParam;
import edu.eas.common.pojo.OauthClientDetails;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cql
 */
@RestController
@Api(tags = "OauthClientDetailsController", description = "客户端管理")
@RequestMapping("/client")
public class OauthClientDetailsController {

    @Autowired
    private OauthClientDetailsService oauthClientDetailsService;

    @ApiOperation(value = "删除凭证")
    @DeleteMapping("{clientId}")
    public CommonResult delete(@PathVariable("clientId") String clientId) {
        int count = oauthClientDetailsService.delete(clientId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "新增凭证")
    @PostMapping
    public CommonResult create(@RequestBody ClientParam param) {
        int count = oauthClientDetailsService.create(param);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "更新凭证")
    @PutMapping
    public CommonResult update(@RequestBody ClientParam param) {
        int count = oauthClientDetailsService.update(param);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation(value = "分页查询客户端数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = false),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false),
            @ApiImplicitParam(name = "clientId", value = "客户端标识", required = false),
            @ApiImplicitParam(name = "authorizedGrantTypes", value = "授权类型", required = false)
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<OauthClientDetails>> getList(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value = "clientId", defaultValue = "") String clientId,
                                                                @RequestParam(value = "authorizedGrantTypes", defaultValue = "") String authorizedGrantTypes) {
        List<OauthClientDetails> result = oauthClientDetailsService.getList(pageSize, pageNum, clientId, authorizedGrantTypes);
        return CommonResult.success(CommonPage.restPage(result));
    }

    @ApiOperation(value = "获取客户端详情")
    @GetMapping("/{clientId}")
    public CommonResult<OauthClientDetails> getOauthClientDetail(@PathVariable("clientId") String clientId) {
        OauthClientDetails detail = oauthClientDetailsService.getOauthClientDetailByClientId(clientId);
        return CommonResult.success(detail);
    }


}
